package com.x.provider.customer.factory.dto.impl;

import com.x.core.domain.SuggestionTypeEnum;
import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerResumeDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.customer.configure.CustomerApplicationConfig;
import com.x.provider.customer.enums.SystemResumeAttributeName;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.model.domain.common.GenericAttribute;
import com.x.provider.customer.service.customer.CustomerResumeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service("customerResumeBuilder")
public class CustomerResumeBuilderImpl implements CustomerDTOBuilder {

   private final CustomerResumeService customerResumeService;
   private final CustomerApplicationConfig applicationConfig;

   public CustomerResumeBuilderImpl(CustomerResumeService customerResumeService,
                                    CustomerApplicationConfig applicationConfig){
      this.customerResumeService = customerResumeService;
      this.applicationConfig = applicationConfig;
   }

   @Override
   public void build(CustomerListReqDTO listCustomerAO, Map<Long, CustomerDTO> dest) {
      if (!listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER_RESUME.name())){
         return;
      }
      List<CustomerResumeDTO> resumeList = BeanUtil.prepare(customerResumeService.listResume(listCustomerAO.getCustomerIds()), CustomerResumeDTO.class);
      Map<Long, List<CustomerResumeDTO>> customerResumeMap = resumeList.stream().collect(Collectors.groupingBy(item -> item.getCustomerId()));
      Map<Long, List<GenericAttribute>> resumeAttribute = customerResumeMap.isEmpty() ? new HashMap<>() : customerResumeService.listResumeAttribute(resumeList.stream().map(opt -> opt.getId()).collect(Collectors.toList()), null);
      dest.entrySet().forEach(item -> {
         List<CustomerResumeDTO> customerResume = customerResumeMap.get(item.getKey());
         if (CollectionUtils.isEmpty(customerResume)){
            return;
         }
         customerResume.stream().forEach(opt ->{
            List<GenericAttribute> genericAttributes = resumeAttribute.get(opt.getId());
            prepare(opt, genericAttributes);
         });
         item.getValue().setCustomerResumeList(customerResume);
      });
      prepareDefault(dest);
   }

   private void prepareDefault(Map<Long, CustomerDTO> dest){
      dest.values().forEach(item ->{
         if (CollectionUtils.isEmpty(item.getCustomerResumeList())){
            item.setCustomerResumeList(new ArrayList<>());
            item.getCustomerResumeList().add(CustomerResumeDTO.builder().build());
         }
         item.getCustomerResumeList().forEach(opt ->{
            prepareDefault(opt);
         });
      });
   }

   private void prepareDefault(CustomerResumeDTO customerResumeDTO){
      if (StringUtils.isEmpty(customerResumeDTO.getNickName())) {
         customerResumeDTO.setNickName(applicationConfig.getDefaultNickName());
      }
      if (StringUtils.isEmpty(customerResumeDTO.getAvatarId())){
         customerResumeDTO.setAvatarId(applicationConfig.getDefaultAvatarId());
      }
   }

   private void prepare(CustomerResumeDTO customerResume, List<GenericAttribute> attributeList){
      if (customerResume == null || CollectionUtils.isEmpty(attributeList)){
         return;
      }
      Map<String, GenericAttribute> attributeMap = attributeList.stream().collect(Collectors.toMap(item -> item.getKey(), item -> item));
      attributeMap.entrySet().forEach(item ->{
         prepareNickName(customerResume, item);
         prepareSignature(customerResume, item);
         prepareAvtarId(customerResume, item);
      });
   }

   private void prepareAvtarId(CustomerResumeDTO customerResume, Map.Entry<String, GenericAttribute> item) {
      if (SystemResumeAttributeName.AVATAR_ID.name().equals(item.getKey())){
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.PASS.getValue())){
            customerResume.setAvatarId(item.getValue().getValue());
         }
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.REVIEW.getValue())){
            customerResume.setAvatarIdReview(item.getValue().getValue());
         }
      }
   }

   private void prepareSignature(CustomerResumeDTO customerResume, Map.Entry<String, GenericAttribute> item) {
      if (SystemResumeAttributeName.SIGNATURE.name().equals(item.getKey())){
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.PASS.getValue())){
            customerResume.setSignature(item.getValue().getValue());
         }
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.REVIEW.getValue())){
            customerResume.setSignatureReview(item.getValue().getValue());
         }
      }
   }

   private void prepareNickName(CustomerResumeDTO customerResume, Map.Entry<String, GenericAttribute> item) {
      if (SystemResumeAttributeName.NICK_NAME.name().equals(item.getKey())){
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.PASS.getValue())){
            customerResume.setNickName(item.getValue().getValue());
         }
         if (item.getValue().getSuggestionType().equals(SuggestionTypeEnum.REVIEW.getValue())){
            customerResume.setNickNameReview(item.getValue().getValue());
         }
      }
   }

}
